/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.biz.domain.ProfessionDetail;
import me.zhengjie.modules.biz.domain.dto.UniversityDetailDTO;
import me.zhengjie.modules.biz.domain.resp.ProfessionDetailResp;
import me.zhengjie.modules.biz.domain.vo.ProfessionQueryCriteria;
import me.zhengjie.modules.biz.mapper.UniversitiesMapper;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.ProfessionDetailService;
import me.zhengjie.modules.biz.domain.vo.ProfessionDetailQueryCriteria;
import me.zhengjie.modules.biz.mapper.ProfessionDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-03-19
**/
@Service
@RequiredArgsConstructor
public class ProfessionDetailServiceImpl extends ServiceImpl<ProfessionDetailMapper, ProfessionDetail> implements ProfessionDetailService {

    private final ProfessionDetailMapper professionDetailMapper;

    private final UniversitiesMapper universitiesMapper;

    @Override
    public PageResult<ProfessionDetail> queryAll(ProfessionDetailQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(professionDetailMapper.findAll(criteria, page));
    }

    @Override
    public List<ProfessionDetail> queryAll(ProfessionDetailQueryCriteria criteria){
        return professionDetailMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProfessionDetail resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProfessionDetail resources) {
        ProfessionDetail professionDetail = getById(resources.getId());
        professionDetail.copy(resources);
        saveOrUpdate(professionDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<ProfessionDetail> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProfessionDetail professionDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("专业名称", professionDetail.getProfessionName());
            map.put("专业类型【0：本科 1：专科】", professionDetail.getProfessionType());
            map.put("专业介绍", professionDetail.getProfessionDetail());
            map.put("主要课程", professionDetail.getProfessionSubject());
            map.put("就业方向", professionDetail.getJobDirection());
            map.put("学位", professionDetail.getDegree());
            map.put("考研方向", professionDetail.getPostgraduateDirection());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 专业详情
     * @param professionName
     * @return
     */
    @Override
    public ProfessionDetailResp getProfessionDetail(String professionName) {
        ProfessionDetailResp resp = new ProfessionDetailResp();
        // 查询专业详情
        ProfessionDetail professionDetail = professionDetailMapper.selectOne(new QueryWrapper<ProfessionDetail>()
                .eq("profession_name",professionName));
        // 查询相关院校
        if(professionDetail != null){
            List<UniversityDetailDTO>  associateUniversities= universitiesMapper.getAssociateUniversities(professionName);
            // 设置返回结果
            resp.setProfessionDetail(professionDetail);
            resp.setUniversityList(associateUniversities);
        }
        return resp;
    }
}